package im.device.config.widget;

import android.content.Context;
import android.location.Location;
import app.log.AppLog;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKGeocoderAddressComponent;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;

/**
 * 定位类:
 * 通过经纬度反转查询相应城市名
 * @author EX-NINGZHIMENG001
 *
 */
public class LocationPointAction {
	private static final String TAG = LocationPointAction.class.getSimpleName();
	
	private BMapApiApplication app;
	private LocationListener mLocationListener = null;
	private MKSearch mSearch = null;
	private String city;
	private StringBuffer address;
	private MyLocationListener mMyLocationListener;
	
	/**
	 * 初始化location监听
	 * @param activity
	 */
	public void initLocationListener(Context activity) {
//		app = (BMapApiApplication)activity.getApplication();
		app = BMapApiApplication.mBMapApiApplication;
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(activity);
			app.mBMapMan.init(app.mStrKey, new BMapApiApplication.MyGeneralListener());
		}
		initBaiduViews();
		
		// location监听
        mLocationListener = new LocationListener(){

			@Override
			public void onLocationChanged(Location location) {
				app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
				if (location != null){
					GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6),
							(int)(location.getLongitude()*1e6));
					if(mMyLocationListener != null){
						mMyLocationListener.getLocation(location);
					}
					// 通过经纬度反转查询城市名
					mSearch.reverseGeocode(pt);
					AppLog.e(TAG, "进入location监听...");
				}
			}
        };
	}
	
	private void initBaiduViews() {
		// 初始化MKSearch
        mSearch = new MKSearch();
        mSearch.init(app.mBMapMan, new MKSearchListener(){
			public void onGetAddrResult(MKAddrInfo res, int error) {
				if (error != 0) {
					return;
				}

				MKGeocoderAddressComponent kk = res.addressComponents;
				city = kk.city;
				address = new StringBuffer();
				address.append(city).append(kk.district).append(kk.street).append(kk.streetNumber);
				if(mMyLocationListener!=null){
					mMyLocationListener.getLocalCity(city);
					mMyLocationListener.getLocalAddress(address.toString());
				}
				AppLog.e(TAG, "当前城市:" + city + " 详细地址:" + address.toString());
			}
			public void onGetPoiResult(MKPoiResult res, int type, int error) {
				if (error != 0 || res == null) {
					return;
				}
				if (res != null && res.getCurrentNumPois() > 0) {
					GeoPoint ptGeo = res.getAllPoi().get(0).pt;
				}
			}
			public void onGetDrivingRouteResult(MKDrivingRouteResult res,
					int error) {
			}
			public void onGetTransitRouteResult(MKTransitRouteResult res,
					int error) {
			}
			public void onGetWalkingRouteResult(MKWalkingRouteResult res,
					int error) {
			}
			public void onGetBusDetailResult(MKBusLineResult result, int iError) {
			}
			@Override
			public void onGetSuggestionResult(MKSuggestionResult res, int arg1) {
			}

        });
	}
	
	/**
	 * 注销Location监听
	 * @param activty
	 */
	public void removeListener(Context activty){
		BMapApiApplication app = BMapApiApplication.mBMapApiApplication;
		app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
		app.mBMapMan.stop();
	}
	
	/**
	 * 注册Location监听
	 * @param activty
	 */
	public void requestLocationListener(Context activty){
		BMapApiApplication app = BMapApiApplication.mBMapApiApplication;
        app.mBMapMan.getLocationManager().requestLocationUpdates(mLocationListener);
		app.mBMapMan.start();
	}
	
	/**
	 * 接口回调
	 * @author EX-NINGZHIMENG001
	 *
	 */
	public static interface MyLocationListener{
		public void getLocation(Location location);
		public void getLocalCity(String city);
		public void getLocalAddress(String address);
	}
	
	public void setMyLocationListener(MyLocationListener listener){
		this.mMyLocationListener = listener;
	}
}
